﻿Imports NHibernate
Imports NHibernate.Criterion

Public Class TeamRepository

    Public Sub SaveTeamResult(ByVal mySession As ISession, ByVal round As Integer, ByVal id As Integer, ByVal result As String)
        Dim team = mySession.Get(Of TeamObj)(id)
        If team Is Nothing Then
            Exit Sub
        End If

        Select Case result
            Case "Win"
                team.StillAlive = 1
                Select Case round
                    Case 1
                        team.Round1Result = True
                    Case 2
                        team.Round2Result = True
                    Case 3
                        team.Round3Result = True
                    Case 4
                        team.Round4Result = True
                    Case 5
                        team.Round5Result = True
                    Case 6
                        team.Round6Result = True
                End Select
            Case "Loss"
                team.StillAlive = 0
                Select Case round
                    Case 1
                        team.Round1Result = False
                    Case 2
                        team.Round2Result = False
                    Case 3
                        team.Round3Result = False
                    Case 4
                        team.Round4Result = False
                    Case 5
                        team.Round5Result = False
                    Case 6
                        team.Round6Result = False
                End Select
            Case Else
                Select Case round
                    Case 1
                        team.Round1Result = Nothing
                    Case 2
                        team.Round2Result = Nothing
                    Case 3
                        team.Round3Result = Nothing
                    Case 4
                        team.Round4Result = Nothing
                    Case 5
                        team.Round5Result = Nothing
                    Case 6
                        team.Round6Result = Nothing
                End Select
        End Select

        mySession.Save(team)
    End Sub

    Public Function FindAllTeams(ByVal year As Integer) As IList(Of TeamObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(TeamObj))
            crit.Add(Restrictions.Eq("SurvivorYear", year))
            crit.AddOrder(Order.Asc("Seed"))
            crit.AddOrder(Order.Asc("Region"))
            Dim myList = crit.List(Of TeamObj)()
            Return myList
        End Using
    End Function

    Public Function FindRegionForTeam(ByVal teamId As Integer) As String
        Using mySession = Persister.OpenSession()
            Dim thisTeam = mySession.Get(Of TeamObj)(teamId)
            If thisTeam IsNot Nothing Then
                Return thisTeam.Region
            Else
                Return ""
            End If
        End Using
    End Function

    Public Function FindAllAliveTeams(ByVal year As Integer) As IList(Of TeamObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(TeamObj))
            crit.Add(Restrictions.Eq("SurvivorYear", year))
            crit.Add(Restrictions.Eq("StillAlive", True))
            crit.AddOrder(Order.Asc("Seed"))
            crit.AddOrder(Order.Asc("Region"))
            Dim myList = crit.List(Of TeamObj)()
            Return myList
        End Using
    End Function

    Public Function FindAllTeamsForSpecificRound(ByVal year As Integer, ByVal round As Integer) As IList(Of TeamObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(TeamObj))
            crit.Add(Restrictions.Eq("SurvivorYear", year))

            Select Case round
                Case 2
                    crit.Add(Restrictions.Eq("Round1Result", True))
                Case 3
                    crit.Add(Restrictions.Eq("Round2Result", True))
                Case 4
                    crit.Add(Restrictions.Eq("Round3Result", True))
                Case 5
                    crit.Add(Restrictions.Eq("Round4Result", True))
                Case 6
                    crit.Add(Restrictions.Eq("Round5Result", True))
            End Select

            crit.AddOrder(Order.Asc("Region"))
            crit.AddOrder(Order.Asc("Seed"))
            Dim myList = crit.List(Of TeamObj)()
            Return myList
        End Using
    End Function
End Class
